package com.doyutu.seed.utils;

import com.google.common.util.concurrent.RateLimiter;

/**
 * @author DoyuTu
 * @version 0.0.1
 * spring-boot-project-seed
 * 单机Guava令牌桶算法JVM级别限流器
 */
public class RateLimiterUtil {

    /**
     * 指定每秒放置的令牌数
     */
    public static RateLimiter createLimiter(double permitsPerSecond) {
        return RateLimiter.create(permitsPerSecond);
    }

    /**
     * 获取指定数量的令牌，否则将等待
     *
     * @return 等待的秒数
     */
    public static Double acquire(RateLimiter rateLimiter, int permits) {
        return rateLimiter.acquire(permits);
    }

}
